#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
LIS系统依赖检查脚本
快速检查项目依赖是否已正确安装
"""

import sys
import importlib
import platform
import os

def check_module(module_name, package_name=None):
    """检查模块是否可用"""
    if package_name is None:
        package_name = module_name
    
    try:
        importlib.import_module(module_name)
        print(f"✅ {package_name}")
        return True
    except ImportError:
        print(f"❌ {package_name}")
        return False

def check_python_version():
    """检查Python版本"""
    version = sys.version_info
    print(f"Python版本: {version.major}.{version.minor}.{version.micro}")
    
    if version < (3, 8):
        print("❌ Python版本过低，需要 >= 3.8")
        return False
    else:
        print("✅ Python版本符合要求")
        return True

def check_system_info():
    """检查系统信息"""
    print(f"操作系统: {platform.system()} {platform.release()}")
    print(f"架构: {platform.machine()}")
    print(f"Python路径: {sys.executable}")
    print()

def check_core_dependencies():
    """检查核心依赖"""
    print("🔍 检查核心依赖:")
    print("-" * 40)
    
    core_modules = [
        ("PyQt6", "PyQt6"),
        ("pandas", "pandas"),
        ("openpyxl", "openpyxl"),
        ("reportlab", "reportlab"),
        ("PIL", "Pillow"),
        ("cryptography", "cryptography"),
        ("psutil", "psutil")
    ]
    
    all_ok = True
    for module_name, package_name in core_modules:
        if not check_module(module_name, package_name):
            all_ok = False
    
    print()
    return all_ok

def check_dev_dependencies():
    """检查开发依赖"""
    print("🔍 检查开发依赖:")
    print("-" * 40)
    
    dev_modules = [
        ("pytest", "pytest"),
        ("black", "black"),
        ("flake8", "flake8"),
        ("mypy", "mypy"),
        ("sphinx", "sphinx")
    ]
    
    all_ok = True
    for module_name, package_name in dev_modules:
        if not check_module(module_name, package_name):
            all_ok = False
    
    print()
    return all_ok

def check_fonts():
    """检查中文字体"""
    print("🔍 检查中文字体:")
    print("-" * 40)
    
    if platform.system() == "Windows":
        font_paths = [
            ("SimSun", "C:/Windows/Fonts/simsun.ttc"),
            ("SimSun", "C:/Windows/Fonts/simsun.ttf"),
            ("SimHei", "C:/Windows/Fonts/simhei.ttf"),
            ("Microsoft YaHei", "C:/Windows/Fonts/msyh.ttf"),
            ("KaiTi", "C:/Windows/Fonts/simkai.ttf")
        ]
        
        found_fonts = []
        for font_name, font_path in font_paths:
            if os.path.exists(font_path):
                found_fonts.append(font_name)
                print(f"✅ {font_name}")
            else:
                print(f"❌ {font_name}")
        
        if found_fonts:
            print(f"\n✅ 找到中文字体: {', '.join(found_fonts)}")
        else:
            print("\n⚠️ 未找到中文字体，PDF导出功能可能受影响")
    else:
        print("ℹ️ 非Windows系统，请手动检查中文字体")
    
    print()

def main():
    """主函数"""
    print("=" * 60)
    print("LIS系统 - 依赖检查")
    print("=" * 60)
    print()
    
    # 检查系统信息
    check_system_info()
    
    # 检查Python版本
    python_ok = check_python_version()
    print()
    
    # 检查核心依赖
    core_ok = check_core_dependencies()
    
    # 检查开发依赖
    dev_ok = check_dev_dependencies()
    
    # 检查字体
    check_fonts()
    
    # 总结
    print("=" * 60)
    print("检查结果:")
    print("=" * 60)
    
    if python_ok and core_ok:
        print("✅ 核心依赖检查通过，系统可以正常运行")
    else:
        print("❌ 核心依赖检查失败，请安装缺失的依赖")
        print("运行: python install_dependencies.py")
    
    if dev_ok:
        print("✅ 开发依赖检查通过，可以进行开发工作")
    else:
        print("⚠️ 开发依赖检查失败，如需开发请安装开发依赖")
        print("运行: pip install -r requirements_dev.txt")
    
    print()
    print("📋 下一步:")
    if python_ok and core_ok:
        print("1. 运行主程序: python scripts/main.py")
        print("2. 运行测试: python -m pytest test/")
    else:
        print("1. 安装依赖: python install_dependencies.py")
        print("2. 或手动安装: pip install -r requirements_production.txt")

if __name__ == "__main__":
    main()
